package DAO;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import model.Message;
import model.model_test;


public class TotalDao 
{
	private UserDao userDao;
	private RoomDao roomDao;
	private MessageDAO messageDAO;
	private ChatDao chatDao;
	private static final Logger logger = Logger.getLogger(TotalDao.class.getName()); // 日志库
	public TotalDao(Connection conn) {
		userDao = new UserDao(conn);
		roomDao = new RoomDao(conn);
		messageDAO = new MessageDAO(conn);
		chatDao = new ChatDao(conn);
        // 创建一个文件处理器
        try {
        	String path = "." + File.separator + "log" + File.separator + "TotalDao.log"; //   ./log/xxx.log
            FileHandler fileHandler = new FileHandler(path);
            fileHandler.setLevel(Level.INFO);

            // 创建一个日志格式化器
            Formatter formatter = new SimpleFormatter();
            fileHandler.setFormatter(formatter);

            // 将处理器添加到日志记录器
            logger.addHandler(fileHandler);
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
	
	// 注册用户(传入name和password,传出userId)
	public int userRegister(String theName, String thePassword) {
		logger.info("用户注册");
		return userDao.createUser(theName, thePassword);
	}
	
	// 登录(传入name和password,传出userId) >0 正常id  == -1密码错误    ==  -2账号不存在
	public int userLogin(int theName, String thePassword) {
		logger.info("登录");
		return userDao.loginGetID(theName, thePassword);
	}
	
	// 根据userid获得所有房间名和房间号
	public Hashtable<Integer, String> userRooms(int userId) {
		logger.info("根据userid获得所有房间名和房间号");
		Vector<Integer> roomIds = userDao.getAllRoom(userId);
		//Vector<String> roomNames = new Vector<>();
		Hashtable<Integer, String> rooms = new Hashtable<Integer, String>();
		
		for(Integer i : roomIds) {
			//roomNames.add(roomDao.getNameById(roomIds.elementAt(i)));
			int roomAccount = roomDao.getAccountById(i);
			String roomName = roomDao.getNameById(i);
			rooms.put(roomAccount, roomName);
		}
		return rooms;
	}
	
	//根据账号获取userid
	public int getUserId(int account) {
		logger.info("根据账号获取userid");
		return userDao.getUserId(account);
	}
	
	//根据userid获取账号
	public int getUserAccount(int userid) {
		logger.info("根据userid获取账号");
		return userDao.getUserAccount(userid);
	}
	
	//根据账号获得用户昵称
	public String getUserName(int useraccount) {
		logger.info("根据账号获得用户昵称");
		return userDao.getUserName(useraccount);
	}
	
	// 根据账号获得所有的好友
	public Hashtable<Integer, String> userFriends(int useraccount){
		logger.info("根据账号获得所有的好友");
		return userDao.getAllFriends(useraccount);
	}
	
	// 通过roomName/roomId获得历史信息
	public Vector<model.Message> roomMessages(String roomName) {
		logger.info("通过roomName/roomId获得历史信息");
		int roomId = roomDao.getIdByName(roomName);
		return messageDAO.Query(roomId);
	}
	
	// 将消息存入数据库
	public boolean insertMessage(int roomId, int userId, String message) {
		logger.info("将消息存入数据库");
		return messageDAO.Insert(userId, roomId, message);
	}
	
	// 通过roomName/roomId获得历史信息
		public Vector<model.Message> roomMessages(int roomId) {
			logger.info("通过roomName/roomId获得历史信息");
			return messageDAO.Query(roomId);
		}
				
		
	// 退出群聊(通过群聊名称来获得)
	public boolean leaveRoom(int userId, String roomName) {
		logger.info("退出群聊");
		int roomId = roomDao.getIdByName(roomName);
		roomDao.leaveTheRoom(userId, roomId);
		userDao.leaveTheRoom(userId, roomId);
		return true;
	}
	
	//添加好友
	public boolean addFriend(int myaccount,int faccount,String fname,String myname) {
		logger.info("添加好友");
		String result1 = messageDAO.addFriend(myaccount, faccount, fname, myname);
		int myid = userDao.getUserId(myaccount);
		int fid = userDao.getUserId(faccount);
		int roomid = roomDao.createRoom(myid, null);
		int result2 = roomDao.joinNewRoom(fid, roomid,myaccount,faccount);
		
		if(result1.equals("ok") && result2 == 0) {
			return true;
		}
		
		return false;
	}
	
	//删除好友
	public boolean deleFriend(int maccount,int faccount) {
		boolean result1 = messageDAO.deleFriend(maccount, faccount);
		String roomaccount = String.valueOf(maccount)+String.valueOf(faccount);
		int roomId = roomDao.getIdByLongAccount(Long.parseLong(roomaccount));
		boolean result2 = roomDao.deleteRoom(roomId);
		
		if(result1 && result2) {
			return true;
		}
		return false;
	}
	
	//退出群聊
	public boolean exitGroup(int maccount,Long roomaccount) {
		int userId = userDao.getUserId(maccount);
		int roomId = roomDao.getIdByLongAccount(roomaccount);
		boolean result1 = roomDao.leaveTheRoom(userId, roomId);
		boolean result2 = userDao.leaveTheRoom(userId, roomId);
		
		if(result1 && result2) {
			return true;
		}
		return false;
	}
	
	// 通过userId获得聊天机器人历史信息
	public Vector<model.Message> machineMessages(int userId) {
		logger.info("通过userId获得聊天机器人历史信息");
		return chatDao.Query(userId);
	} 
	
	// 插入聊天机器人信息
	public boolean machineInsert(int userId, int isMachine, String mes) {
		logger.info("插入聊天机器人信息");
		return chatDao.Insert(userId, isMachine, mes);
	}
}